﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using HR60_PanelSoft.Tools;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.TabForms;

namespace HR60_PanelSoft.View.Controls
{
	public class FormComboBox : BaseFormControl
	{
		public static readonly object VALUE_NULL = new object();

		private ComboBox comboBox = new ComboBox();

		// 默认静态选项,常作标题用
		private SelectItem firstItem = null;
		
		public FormComboBox()
		{
			comboBox.Margin = new Padding(0);
			comboBox.Dock = DockStyle.Fill;
			comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
			comboBox.SelectedValueChanged += new EventHandler(valueChange);

			this.AutoSize = false;
			this.Width = 121;
			this.Height = 20;
			this.Controls.Add(comboBox);
		}

		public System.Windows.Forms.ComboBox.ObjectCollection Items
		{
			get
			{
				return comboBox.Items;
			}
		}

		/// <summary>
		/// 当前显示的文本
		/// </summary>
		public new string Text
		{
			set
			{
				comboBox.SelectedText = value;
			}
			get
			{
				return comboBox.SelectedText;
			}
		}

		/// <summary>
		/// 设置静态选项
		/// </summary>
		/// <param name="item"></param>
		public void setStaticFirstItem(SelectItem item)
		{
			firstItem = item;
		}

		/// <summary>
		/// 设置静态选项
		/// </summary>
		/// <param name="text"></param>
		public void setStaticFirstItem(string text)
		{
			SelectItem item = new SelectItem(text, VALUE_NULL);
			firstItem = item;
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		public void bindData(IDictionary data)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (DictionaryEntry de in data)
			{
				list.Add(new SelectItem(de.Key.ToString(), de.Value));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		/// <param name="txtField"></param>
		/// <param name="valField"></param>
		public void bindData(IList data, string txtField, string valField)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				IDictionary<string, object> ps = RefHelper.getFields(de);
				list.Add(new SelectItem(ps[txtField].ToString(), ps[valField]));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		/// <param name="txtField"></param>
		/// <param name="valField"></param>
		public void bindData(IList data, string txtField)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				IDictionary<string, object> ps = RefHelper.getFields(de);
				list.Add(new SelectItem(ps[txtField].ToString(), de));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定数据
		/// </summary>
		/// <param name="data"></param>
		public void bindData(object[] data)
		{
			List<SelectItem> list = new List<SelectItem>();
			foreach (object de in data)
			{
				list.Add(new SelectItem(de.ToString(), de));
			}
			bindItems(list);
		}

		/// <summary>
		/// 绑定选项列表
		/// </summary>
		/// <param name="list"></param>
		public void bindItems(List<SelectItem> list)
		{
			if (firstItem != null)
			{
				list.Insert(0, firstItem);
			}
			comboBox.DisplayMember = "text";
			comboBox.ValueMember = "value";
			comboBox.DataSource = list;
		}
		
		/// <summary>
		/// 清除选择项
		/// </summary>
		public void cleanItems()
		{
			comboBox.DataSource = null;
			comboBox.Items.Clear();
		}

		// 重写Value属性
		public override object Value
		{
			get
			{
				return this.comboBox.SelectedValue == VALUE_NULL ? null : this.comboBox.SelectedValue;
			}
			set
			{
				this.comboBox.SelectedValue = value == null ? VALUE_NULL : value;
			}
		}

	}
}
